package com.zzj.aop;

import com.zzj.entity.Post;
import com.zzj.service.IPostService;
import jakarta.annotation.Resource;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ViewCountAop {


    @Resource
    private IPostService postService;

    @Pointcut("@annotation(com.zzj.annotation.ViewCountAnnotation)")
    public void viewCountAopPointcut() {}

    @After("viewCountAopPointcut()")
    public void incrementViewCount(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        Long postId = Long.valueOf(args[0].toString());
        postService.lambdaUpdate()
                .eq(Post::getId, postId)
                .setSql("views = views + 1").update();

    }

}
